/**
 * @description 路由拦截状态管理，目前两种模式：all模式与intelligence模式，其中partialRoutes是菜单暂未使用
 */
import { asyncRoutes, constantRoutes } from "@/router";
import { getRouterList } from "@/api/router";
import { filterAllRoutes, filterAsyncRoutes } from "@/utils/handleRoutes";
import { getMenutree } from "@/api/user";
import store from "@/store";

const state = { routes: [], partialRoutes: [] };
const getters = {
	routes: (state) => state.routes,
	partialRoutes: (state) => state.partialRoutes,
};
const mutations = {
	setRoutes(state, routes) {
		state.routes = constantRoutes.concat(routes);
	},
	setAllRoutes(state, routes) {
		state.routes = constantRoutes.concat(routes);
	},
	setPartialRoutes(state, routes) {
		state.partialRoutes = constantRoutes.concat(routes);
	},
};
const actions = {
	async setRoutes({ commit }, permissions) {
		// let accessedRoutes = [];
		// if (permissions.includes("mark")) {
		// 	accessedRoutes = asyncRoutes;
		// } else {
		// 	accessedRoutes = await filterAsyncRoutes(asyncRoutes, permissions);
		// }
		commit("setRoutes", permissions);
		return permissions;
	},
	async setAllRoutes({ commit, state, dispatch }) {
		// getMenutree
		// getRouterList
		let { data } = await getMenutree(state.accessToken);
		data = data || []
		data.unshift({ "path": "/", "component": "Layout", "redirect": "index", "children": [ { "path": "/index", "name": "Index", "component": "index/index", "meta": { "title": "首页", "icon": "home", "affix": true, "show":"0", } } ] })
		data.push({ path: "/personalCenter", component: 'Layout', hidden: true, redirect: "personalCenter", children: [ { path: "personalCenter", name: "PersonalCenter", component: "views/personalCenter/index", meta: { title: "个人中心", }, }, ], })
		data.push({ path: "*", redirect: "/404", hidden: true });
		let accessRoutes = filterAllRoutes(data);
		commit("setAllRoutes", accessRoutes);
		await store.dispatch("user/getButtonJurisdiction");
		return accessRoutes;
	},
	setPartialRoutes({ commit }, accessRoutes) {
		commit("setPartialRoutes", accessRoutes);
		return accessRoutes;
	},
};
export default { state, getters, mutations, actions };
